<?php
namespace app\common\event;

class Base {
    protected $module = '';

    public function __construct($module = 'common') {
        $this->module = $module;
    }

    public function trigger($name, $args = []) {
        if(is_array($name)) {
            $event = loader($this->module)->loadAny('event', $name[0]);
            $fun = $name[1];
        } else {
            $filter = config('event');
            if(isset($filter[$name])) {
                return trigger($filter[$name], $args);
            }
            $event = null;
            $fun = $name;
        }

        if($event && method_exists($event, $fun)) {
            return $event->$fun(...$args);
        } else {
            $event = loader($this->module)->loadAny('event', 'Event');
            if(method_exists($event, $fun)) {
                return $event->$fun(...$args);
            }
        }
        return null;
    }
}
